package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.eecs.entity.Academy;
import com.cqjtu.eecs.entity.Major;
import com.cqjtu.eecs.entity.dto.MajorDTO;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.mapper.AcademyMapper;
import com.cqjtu.eecs.mapper.MajorMapper;
import com.cqjtu.eecs.service.IMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjtu.eecs.utils.Assert;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luojun
 * @since 2022-02-28
 */
@Service
@NoArgsConstructor
@AllArgsConstructor
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements IMajorService {
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private MajorMapper majorMapper;

    @Override
    public List<MajorDTO> getMajorListByAcademyId(String id) {
        List<Major> majorList = list(new LambdaQueryWrapper<Major>()
                .eq(Major::getAcademyId, id)
        );
        // 获取学院
        Academy academy = academyMapper.selectById(id);
        Assert.notNull(academy, new NotFoundException("不存在该学院"));
        List<MajorDTO> majorDTOList = new ArrayList<>();
        majorList.forEach(item -> {
            majorDTOList.add(new MajorDTO(item, academy));
        });
        return majorDTOList;
    }

    @Override
    public List<MajorDTO> getMajorList() {
        // 获取全部学院
        List<Academy> academies = academyMapper.selectList(null);
        Map<String, Academy> map = new HashMap<>();
        academies.forEach(item->{
            map.put(item.getAcademyId(), item);
        });
        // 获取全部专业
        List<Major> majorList = list();
        List<MajorDTO> majorDTOS = new ArrayList<>();
        majorList.forEach(item -> {
            majorDTOS.add(new MajorDTO(item, map.get(item.getAcademyId())));
        });
        return majorDTOS;
    }

    @Override
    public List<String> selectAllMajorCodeByAcademyId(String academyId) {
        return majorMapper.selectAllMajorCodeByAcademyId(academyId);
    }
}
